<template>
  <div>
    <el-dialog
            v-model="dialogVisible"
            :title="id ? $t('人员编辑') : $t('人员录入')"
            width="643"
            @close="closeAddMember"
    >
      <el-form :model="form" :rules="rules" ref="formRef">
        <!-- 姓名 -->
        <el-form-item
                :label="$t('姓名')"
                :label-width="formLabelWidth"
                :label-position="labelPosition"
                prop="personName"
        >
          <el-input
                  v-model="form.personName"
                  autocomplete="off"
                  :placeholder="$t('请输入姓名')"
          />
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item
                :label="$t('手机号')"
                :label-width="formLabelWidth"
                :label-position="labelPosition"
                prop="phone"
        >
          <el-input
                  v-model="form.phone"
                  autocomplete="off"
                  :placeholder="$t('请输入手机号')"
          />
        </el-form-item>
        <!-- 电子邮箱 -->
        <el-form-item
                :label="$t('电子邮箱')"
                :label-width="formLabelWidth"
                :label-position="labelPosition"
                prop="email"
        >
          <el-input
                  v-model="form.email"
                  autocomplete="off"
                  :placeholder="$t('请输入电子邮件')"
          />
        </el-form-item>
        <!-- 部门/班级 -->
        <el-form-item
                :label="$t('部门/班级')"
                :label-width="formLabelWidth"
                :label-position="labelPosition"
                prop="deptName"
        >
          <el-input
                  v-model="form.deptName"
                  autocomplete="off"
                  :placeholder="$t('请输入所在部门/班级')"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div department="dialog-footer">
          <el-button @click="dialogVisible = false">{{$t('取消')}}</el-button>
          <el-button type="primary" department="confirm" @click="handleSubmit">
            {{$t('确认')}}
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import { addStuff, editStuff, getStuffInfo } from '@/api/index';

export default {
  props: {
    id: {
      type: [String, Number],
      default: null,
    },
  },
  data() {
    return {
      dialogVisible: true,
      formLabelWidth: '110px',
      labelPosition: 'left',
      form: {
        personName: '',
        phone: '',
        email: '',
        deptName: '',
      },
      rules: {
        // 姓名校验规则
        personName: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
        ],
        // 手机号校验规则
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入有效的手机号',
            trigger: 'blur',
          },
        ],
        // 电子邮箱校验规则
        email: [
          { required: true, message: '电子邮箱不能为空', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入有效的电子邮箱',
            trigger: 'blur',
          },
        ],
        // 部门/班级校验规则
        deptName: [
          { required: true, message: '部门/班级不能为空', trigger: 'blur' },
        ],
      },
    };
  },
  mounted() {
    if (this.id) {
      this.fetchEmployeeDetails(this.id);
    }
  },
  methods: {
    // 获取员工详情
    async fetchEmployeeDetails(id) {
      try {
        const { data } = await getStuffInfo(id);
        if (data.code === 0) {
          this.form = data.data;
        } else {
          ElMessage.error('获取员工信息失败');
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 关闭弹窗
    closeAddMember() {
      this.$emit('closeAddMember');
    },
    // 提交表单（新增或编辑）
    async handleSubmit() {
      try {
        // 校验表单
        await this.$refs.formRef.validate();
        // 提交数据
        const response = this.id
          ? await editStuff(this.id, this.form) // 编辑
          : await addStuff(this.form); // 新增

        if (response.data.code === 0) {
          this.$emit('closeAddMember');
          this.$emit('refreshData');
          ElMessage.success(this.id ? '员工信息更新成功' : '员工添加成功');
        }
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>
